package controllers

import (
	"encoding/json"

	"gitee.com/lzqysoft/ga_wz_backend/models"
	"github.com/astaxie/beego"
)

type AppController struct {
	CommonController
}

func (c *AppController) URLMapping() {
	c.Mapping("CheckUpdate", c.CheckUpdate)
	c.Mapping("UpdateApp", c.UpdateApp)
	c.Mapping("GetApps", c.GetApps)
	c.Mapping("GetAppVersion", c.GetAppVersion)
}

// CheckUpdate 检查更新
// @Title CheckUpdate 验证码
// @Description 检查更新
// @Param	channel	query	string	true	"admin"
// @Param	package	  query	  string	true	"admin"
// @Param	version	  query	  int	true	"123"
// @Success 200 {object} models.SysApp
// @Failure 403 channel, package or version is null
// @router /CheckUpdate [get]
func (c *AppController) CheckUpdate() {
	channel := c.GetString("channel")
	appPackage := c.GetString("package")
	versionCode, _ := c.GetInt("versionCode")
	beego.Debug(channel, appPackage, versionCode)
	m, err := models.CheckUpdate(channel, appPackage, versionCode)
	if err != nil {
		beego.Debug(err)
		c.RetError(ErrInputData)
	}
	c.RetSuccess(m)
}

// UpdateApp 上传新版本app
// @Title UpdateApp 检查更新
// @Description User Login by username and pwd
// @Param	channel	query	string	true	"admin"
// @Param	package	  query	  string	true	"admin"
// @Param	version	  query	  int	true	"123"
// @Param	url	  query	  string	true	"3,4578214"
// @Success 200 {object} models.SysApp
// @Failure 403 channel, package or version is null
// @router /UpdateApp [post]
func (c *AppController) UpdateApp() {
	tdata, err := c.AesDecodeToByte(c.GetString("tdata"))
	if err != nil {
		beego.Debug(err)
		c.RetError(ErrToken)
	}
	var appInfo VersionInfo
	if err = json.Unmarshal(tdata, &appInfo); err != nil {
		beego.Debug(err)
		c.RetError(ErrInputData)
	}
	m := models.NewSysApp()
	m.Channel = appInfo.Channel
	m.Package = appInfo.Package
	m.VersionCode = appInfo.VersionCode
	m.Url = appInfo.Url
	m.UpdateContent = appInfo.Content
	m.Md5 = appInfo.MD5
	m.Size = appInfo.Size
	m.VersionName = appInfo.VersionName
	models.AddSysApp(m)
	c.RetSuccess(nil)
}

type VersionInfo struct {
	Channel     string `json:"channel"`
	Package     string `json:"package"`
	VersionCode int    `json:"version_code"`
	Url         string `json:"url"`
	Content     string `json:"content"`
	MD5         string `json:"md5"`
	Size        int64  `json:"size"`
	VersionName string `json:"version_name"`
}

// GetAppVersion 获取app版本信息
// @Title GetAppVersion 验证码
// @Description GetAppVersion
// @Param	channel	query	string	true	"admin"
// @Param	package	  query	  string	true	"admin"
// @Success 200 {object} []models.SysApp
// @Failure 403 channel, package or version is null
// @router /GetAppVersion [get]
func (c *AppController) GetAppVersion() {
	channel := c.GetString("channel")
	appPackage := c.GetString("package")
	res, err := models.GetAppVersions(channel, appPackage)
	if err != nil {
		beego.Debug(err)
		c.RetError(ErrDatabase)
	}
	c.RetSuccess(res)
}

// GetApps 获取app
// @Title GetApps 验证码
// @Description GetApps
// @Success 200 {object} []orm.Params
// @router /GetApps [get]
func (c *AppController) GetApps() {
	res, err := models.GroupApps()
	if err != nil {
		beego.Debug(err)
		c.RetError(ErrDatabase)
	}
	c.RetSuccess(res)
}
